home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Firefox Backup Extension 6.0.3 / chrome / febe.jar / content / boxnet.js < prev    next >
Text File  |  2008-09-22  |  18KB  |  502 lines

  1. // author: Chuck Baker
  2. // contact: febe@customsoftwareconsult.com
  3. // Version 6.0
  4.  
  5. var febeBoxKey = "nyusp9iniip8inoiau41uay4ygttlsku";
  6. var febeAuthToken, febeFolderID, febeBoxMessages = [];
  7.  
  8. function febeUploadBackup(){
  9.     febeMsg = window.arguments[0];    // Load messages
  10.     window.document.title = febeMsg[238];
  11.     //window.addEventListener("close",febeUploadClose,true);
  12.     //window.addEventListener("unload",febeUploadClose,true);
  13.     febeUploadAborted = false;
  14.     var results = febeBoxLogin();
  15.     if(results == false)return false;
  16.     var resStatus = febeParseResp(results,"status");
  17.     if(resStatus != "logged"){
  18.         var tmp="<style>color: red; font-weight: bold; font-size: 15px;</style>"+febeMsg[186]+"\n"+ resStatus;
  19.         febeAlert(tmp);
  20.         febeUploadAborted = true;
  21.         return false;
  22.     }//if
  23.     //febeAuthToken = febeParseResp(results,"auth_token");
  24.     febeGetBoxMsg(results);
  25.     try{
  26.         var tmp="<style>color: green; font-weight: bold; font-size: 15px;</style>"+febeMsg[185];
  27.         febeprt(febeMsg[185],"green");
  28.         febeprt(febeBoxMessages[0],"blue");
  29.         febeprt(febeBoxMessages[1],"blue");
  30.         febeprt(febeBoxMessages[2],"blue");
  31.         febeprt(febeBoxMessages[3],"blue");
  32.         febeprt(febeBoxMessages[4],"blue");
  33.         febeprt(febeBoxMessages[5],"blue");
  34.         febeprt(febeBoxMessages[6],"blue");
  35.         febeStartUploadBackup()
  36.     }catch(e){
  37.         var tmp="<style>color: red; font-weight: bold; font-size: 15px;</style>"+febeMsg[8]+"\n"+e;
  38.         febeAlert(tmp);
  39.     }//try/catch
  40.     var box = document.getElementById("messages");
  41.     box.contentDocument.close();
  42.     febeUploadClose();
  43.     return !febeUploadAborted;
  44. }//febeUploadBackup()
  45.  
  46.  
  47. function febeStartUploadBackup(){
  48.     var prefName = "extensions.febe.lastbackup.folder";
  49.     var lastBUfolder = febeGetUnicharPref(prefName);
  50.     if(lastBUfolder == ""){
  51.         febeAlert(febeMsg[9]);
  52.         window.close();
  53.         return false;
  54.     }//if
  55.     febeBuDesDir = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  56.     febeBuDesDir.initWithPath(lastBUfolder);
  57.     febeprt("<br>"+febeMsg[10]+" "+lastBUfolder,"purple");
  58.     febeCreateBoxFolder();
  59.     var errCnt = 0;
  60.     var upCnt = 0;
  61.     var entries = febeBuDesDir.directoryEntries;
  62.     while(entries.hasMoreElements()){
  63.         var entry = entries.getNext();   
  64.         entry.QueryInterface(Ci.nsILocalFile);
  65.         if(entry.isDirectory()) continue;    // Skip folders
  66.         if(febeUpload(entry)){
  67.             upCnt++;
  68.         }else{
  69.             errCnt++;
  70.         }//if
  71.     }//while
  72.     if(errCnt == 0){
  73.         var tmp = febeMsg[11]+" "+upCnt+" "+febeMsg[26];
  74.         febeprt(tmp,"purple");
  75.     }else{
  76.         var tmp = "<style>color: red; font-weight: bold; font-size: 15px;</style>";
  77.         tmp += febeMsg[20];
  78.     }//if
  79.     febeAlert(tmp);
  80.     return true;
  81. }//febeStartUploadBackup()
  82.  
  83. function febeCreateBoxFolder(){
  84.     // Create the backup folder
  85.     var folderName = "FEBE backups"
  86.     var xmlstr = "<?xml version='1.0' encoding='UTF-8' ?>";
  87.     xmlstr += "<request>";
  88.     xmlstr += "<action>create_folder</action>";
  89.     xmlstr += "<api_key>"+febeBoxKey+"</api_key>";
  90.     xmlstr += "<auth_token>"+febeAuthToken+"</auth_token>";
  91.     xmlstr += "<parent_id>0</parent_id>";
  92.     xmlstr += "<name>"+folderName+"</name>";
  93.     xmlstr += "<share>0</share>";  // Set to "1" to publicly share
  94.     xmlstr += "</request>";
  95.  
  96.     var req = new XMLHttpRequest(); 
  97.     req.open('POST', 'http://www.box.net/api/1.0/xml', false); 
  98.     req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  99.     //req.send(xmlstr);
  100.     try{
  101.         req.send(xmlstr);
  102.     }catch(e){
  103.         febeUploadAborted = true;
  104.         return false;
  105.     }//try/catch
  106.     
  107.     var resp = req.responseText; 
  108.     var resStatus = febeParseResp(resp,"status");
  109.     var folderName = febeParseResp(resp,"folder_name");
  110.     febeFolderID = febeParseResp(resp,"folder_id")
  111.     switch(resStatus){
  112.         case "create_ok":
  113.             febeprt(febeMsg[121]+" "+folderName, "green");
  114.             break;
  115.         case "s_folder_exists":
  116.             febeprt(febeMsg[122]+" "+folderName+ "  OK ", "orange");
  117.             break;
  118.         default:
  119.             febeAlert(febeMsg[123]+" " + resStatus);
  120.             return false;
  121.     }//switch    
  122.  
  123.     /*
  124.     // Create subfolder
  125.     var folderName = "FEBE 2008 06-07 05.19.35"
  126.     var xmlstr = "<?xml version='1.0' encoding='UTF-8' ?>";
  127.     xmlstr += "<request>";
  128.     xmlstr += "<action>create_folder</action>";
  129.     xmlstr += "<api_key>"+febeBoxKey+"</api_key>";
  130.     xmlstr += "<auth_token>"+febeAuthToken+"</auth_token>";
  131.     xmlstr += "<parent_id>"+febeFolderID+"</parent_id>";
  132.     xmlstr += "<name>"+folderName+"</name>";
  133.     xmlstr += "<share>0</share>";  // Set to "1" to publicly share
  134.     xmlstr += "</request>";
  135.  
  136.     var req = new XMLHttpRequest(); 
  137.     req.open('POST', 'http://www.box.net/api/1.0/xml', false); 
  138.     req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  139.     //req.send(xmlstr);
  140.     try{
  141.         req.send(xmlstr);
  142.     }catch(e){
  143.         febeUploadAborted = true;
  144.         return false;
  145.     }//try/catch
  146.  
  147.     var resp = req.responseText; 
  148.     var resStatus = febeParseResp(resp,"status");
  149.     switch(resStatus){
  150.         case "create_ok":
  151.             var folderName = febeParseResp(resp,"folder_name")
  152.             febeFolderID = febeParseResp(resp,"folder_id")
  153.             febeprt("Created folder: "+folderName+ "  ID: "+febeFolderID, "green");
  154.             break;
  155.         case "s_folder_exists":
  156.             var febeFolderID = febeParseResp(resp,"folder_id")
  157.             febeprt("Folder already exists: "+folderName+ "  ID: "+febeFolderID, "orange");
  158.             break;
  159.         default:
  160.             alert(febeMsf[123] + resStatus);
  161.     }//switch
  162. */    
  163.     return true;
  164. }//febeCreateBoxFolder()
  165.  
  166. function febeUpload(nsFilename){
  167.     var uploadName = nsFilename.leafName;    
  168.     febeprt("<br>"+febeMsg[21]+" "+uploadName,"purple")
  169.  
  170.     // Buffer the upload file
  171.     var inStream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(Ci.nsIFileInputStream);
  172.     inStream.init(nsFilename, 1, 1, inStream.CLOSE_ON_EOF);
  173.     var bufInStream = Cc["@mozilla.org/network/buffered-input-stream;1"].createInstance(Ci.nsIBufferedInputStream);
  174.     bufInStream.init(inStream, 4096);
  175.     
  176.     //Setup the boundry start stream
  177.     var boundary = "--F-E-B-E--U-p-l-o-a-d-------------" + Math.random();
  178.     var startBoundryStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
  179.     startBoundryStream.setData("\r\n--"+boundary+"\r\n",-1);
  180.     
  181.     // Setup the boundry end stream
  182.     var endBoundryStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(Ci.nsIStringInputStream);
  183.     endBoundryStream.setData("\r\n--"+boundary+"--",-1);
  184.     
  185.     // Setup the mime-stream - the 'part' of a multi-part mime type
  186.     var mimeStream = Cc["@mozilla.org/network/mime-input-stream;1"].createInstance(Ci.nsIMIMEInputStream);
  187.     mimeStream.addContentLength = false;
  188.     mimeStream.addHeader("Content-disposition","form-data; name=\""+uploadName+"\"; filename=\""+uploadName+"\"");
  189.     mimeStream.addHeader("Content-type","multipart/form-data");
  190.     mimeStream.setData(bufInStream);
  191.  
  192.     // Setup a multiplex stream
  193.     var multiStream = Cc["@mozilla.org/io/multiplex-input-stream;1"].createInstance(Ci.nsIMultiplexInputStream);
  194.     multiStream.appendStream(startBoundryStream);
  195.     multiStream.appendStream(mimeStream);
  196.     multiStream.appendStream(endBoundryStream);
  197.     
  198.     var size = Math.round(nsFilename.fileSize / 1024);
  199.     febeprt("Uploading: "+uploadName+"   "+size+"kb");
  200.     var uri = 'http://upload.box.net/api/1.0/upload/'+febeAuthToken+'/'+febeFolderID;
  201.     var req = new XMLHttpRequest(); 
  202.     req.onprogress = onProgress;
  203.     req.open('POST', uri, false); 
  204.     req.setRequestHeader("Content-length",multiStream.available());
  205.     req.setRequestHeader("Content-type","multipart/form-data; boundary="+boundary);
  206.     //req.send(multiStream);
  207.     try{
  208.         req.send(multiStream);
  209.     }catch(e){
  210.         febeUploadAborted = true;
  211.         return false;
  212.     }//try/catch
  213.     var resp = req.responseText; 
  214.     var resStatus = febeParseResp(resp,"status");
  215.  
  216.     switch(resStatus){
  217.         case "upload_ok":
  218.             febeprt(uploadName+" uploaded ok","green");    
  219.             break;
  220.         case "upload_some_files_failed":
  221.             var resp = req.responseText; 
  222.             var domParser = new DOMParser();
  223.             var xmldoc = domParser.parseFromString(resp, "text/xml");
  224.             var response = xmldoc.getElementsByTagName('response')[0];
  225.             var files = response.getElementsByTagName("files")[0].firstChild
  226.             var err = files.getAttribute("error");
  227.             var fname = files.getAttribute("file_name");
  228.             febeprt(febeMsg[124]+" "+fname+": "+err+febeMsg[105], "red");
  229.             return false;
  230.             break;
  231.         default:
  232.             febeAlert(req.responseText);
  233.             return false;
  234.     }//switch    
  235.     return true;
  236. }//febeUpload()
  237.  
  238. function febeprt(txt,color){
  239.     if(!color) color="black";
  240.     var box = document.getElementById("messages");
  241.     txt = "<span style='font-family: Courier New,Courier,monospace; font-size: 12px; color: "+color+";'>"+txt+"<br></span>"
  242.     box.contentDocument.write(txt);
  243.     var scrollBox = box.contentWindow.document.body;
  244.     scrollBox.scrollTop = scrollBox.scrollHeight - scrollBox.clientHeight
  245.     return true;
  246. }//febeprt()
  247.  
  248. function febeParseResp(resp,which){
  249.     var domParser = new DOMParser();
  250.     var xmldoc = domParser.parseFromString(resp, "text/xml");
  251.     var value;
  252.     try{
  253.         value = xmldoc.getElementsByTagName(which)[0].firstChild.nodeValue;
  254.     }catch(e){value = false;}
  255.     return value;
  256. }//febeParseResp
  257.  
  258. function onProgress(e) {
  259.   var percentComplete = (e.position / e.totalSize)*100;
  260.   return
  261. }
  262.  
  263. function febeTestBN(){
  264.   // Test Box.net login - 
  265.     febeSetMsgs();
  266.     var results = febeBoxLogin();
  267.     if(results == false)return false;
  268.     var resStatus = febeParseResp(results,"status");
  269.     if(resStatus != "logged"){
  270.         var tmp="<style>color: red; font-weight: bold; font-size: 15px;</style>"+febeMsg[186]+"\n"+ resStatus;
  271.         febeAlert(tmp);
  272.         return false;
  273.     }//if
  274.     febeGetBoxMsg(results);
  275.     var tmp="<style>color: green; font-weight: bold; font-size: 15px;</style>"+febeMsg[185];
  276.     tmp += "\n"+febeBoxMessages[0]+"\n";
  277.     tmp += "\n"+febeBoxMessages[1];
  278.     tmp += "\n"+febeBoxMessages[2];
  279.     tmp += "\n"+febeBoxMessages[3];
  280.     tmp += "\n"+febeBoxMessages[4];
  281.     tmp += "\n"+febeBoxMessages[5];
  282.     tmp += "\n"+febeBoxMessages[6];
  283.     febeAlert(tmp);
  284.     febeBoxLogout();
  285.     return true;
  286. }//febeTestBN()
  287.  
  288. function febeGetBoxMsg(xml){
  289.     febeBoxMessages = [];
  290.     var login = febeParseResp(xml,"login");
  291.     var email = febeParseResp(xml,"email");
  292.     var user_id = febeParseResp(xml,"user_id");
  293.     var capacity = febeParseResp(xml,"space_amount");
  294.     var space_used = febeParseResp(xml,"space_used");
  295.     var max_upload_size = febeParseResp(xml,"max_upload_size");
  296.     febeBoxMessages.push(febeMsg[192]+" "+login);
  297.     febeBoxMessages.push(febeMsg[193]+" "+email);
  298.     febeBoxMessages.push(febeMsg[194]+" "+user_id);
  299.     
  300.     var mb = 1048576;
  301.     var boxCapacity = parseInt(capacity) / mb;
  302.     boxCapacity = Math.round(boxCapacity * 100);
  303.     boxCapacity = parseInt(boxCapacity) / 100; 
  304.     var spaceUsed = parseInt(space_used) / mb;
  305.     spaceUsed = Math.round(spaceUsed * 100);
  306.     spaceUsed = parseInt(spaceUsed) / 100; 
  307.     var spaceAvailable = boxCapacity - spaceUsed;
  308.     var maxUpload = parseInt(max_upload_size) / mb;
  309.     febeBoxMessages.push(febeMsg[195]+" "+boxCapacity+" MB");
  310.     febeBoxMessages.push(febeMsg[196]+" "+spaceUsed+" MB");
  311.     febeBoxMessages.push(febeMsg[197]+" "+spaceAvailable+" MB");
  312.     febeBoxMessages.push(febeMsg[198]+" "+maxUpload+" MB");
  313.     return true;
  314. }//febeGetBoxMsg()
  315.     
  316. function febeLoginMgrObj(username, password){
  317.     this.hostname = 'chrome://FEBE {Box.net login}'; 
  318.     this.formSubmitURL = 'Box.net login'; 
  319.     this.httprealm = null;
  320.     this.username = username;
  321.     this.password = password;
  322.     var nsLoginInfo = new Components.Constructor("@mozilla.org/login-manager/loginInfo;1", Components.interfaces.nsILoginInfo, "init"); 
  323.     return new nsLoginInfo(this.hostname, this.formSubmitURL, this.httprealm, this.username, this.password, "", "");
  324. }//febeLoginMgrObj()
  325.  
  326. function febeGetBNpassword(username){
  327.   // Return box.net password or null
  328.     var loginManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
  329.     var password; 
  330.     var login = febeLoginMgrObj(username, password)
  331.     
  332.     try { 
  333.         var logins = loginManager.findLogins({}, login.hostname, login.formSubmitURL, login.httprealm); 
  334.         // Find user from returned array of nsILoginInfo objects 
  335.         for (var i = 0; i < logins.length; i++) { 
  336.             if (logins[i].username == username) { 
  337.                 password = logins[i].password; 
  338.                 return password;
  339.                 break; 
  340.             }//if 
  341.         }//for
  342.     } catch(ex) { 
  343.             return null;
  344.     }//try
  345.  
  346. }//febeGetBNpassword()
  347.  
  348. function febeDisableBoxCheck(){
  349.     var enabled = document.getElementById("box_net_enable").checked;
  350.     document.getElementById("box_net_info").hidden = !enabled;
  351.         
  352.     var prefName = "extensions.febe.boxnet.username";
  353.     var uName = febePrefs.getCharPref(prefName);
  354.     document.getElementById("bn_password").value = febeGetBNpassword(uName);
  355.     
  356.     var prefName = "extensions.febe.lastbackup";
  357.     var tmp = febePrefs.getCharPref(prefName);
  358.     var lastBU = false;
  359.     if(tmp != "") lastBU = true;
  360.  
  361.     
  362.     if(uName != ""){    // Disable entry if it already exists - Enabled with "Reset login" button
  363.         document.getElementById("bn_username").disabled = true;
  364.         document.getElementById("bn_password").disabled = true;
  365.         document.getElementById("bn_save").disabled = true;
  366.         document.getElementById("bn_test").disabled = false;
  367.         document.getElementById("bn_upload").disabled = !lastBU;
  368.         document.getElementById("bn_view").disabled = false;
  369.         document.getElementById("box_net_autoupload").disabled = false;
  370.     }else{
  371.         document.getElementById("bn_test").disabled = true;
  372.         document.getElementById("bn_upload").disabled = true;
  373.         document.getElementById("bn_view").disabled = true;
  374.         document.getElementById("bn_save").disabled = false;
  375.         document.getElementById("box_net_autoupload").disabled = true;
  376.         document.getElementById("box_net_autoupload").checked = false;
  377.         document.getElementById("bn_password").value = "";
  378.     }//if
  379.     return true;
  380. }//febeDisableBoxCheck()
  381.  
  382. function febeUploadAbort(){
  383.     var tmp = "<style>color: red; font-weight: bold; font-size: 15px;</style>";
  384.     tmp += febeMsg[8];
  385.     febeAlert(tmp);
  386.     return true;
  387. }//febeUploadAbort()
  388.  
  389. function febeUploadClose(){
  390.     if(febeUploadAborted) febeUploadAbort();
  391.     febeBoxLogout();
  392.     return true;
  393. }//febeUploadClose()
  394.  
  395. function febeBoxLogout(){
  396.     var req = new XMLHttpRequest();
  397.     var str = 'http://www.box.net/api/1.0/rest?action=logout&api_key='+febeBoxKey+'&auth_token='+febeAuthToken;
  398.     req.open('GET',str, false);
  399.     req.send(null);
  400.     var resp = req.responseText;     
  401.     var resStatus = febeParseResp(resp,"status");
  402.     if (resStatus != "logout_ok"){
  403.         var tmp="<style>color: red; font-weight: bold; font-size: 15px;</style>"+febeMsg[190]+"\n"+ febeMsg[189]+" "+resStatus;
  404.         febeAlert(tmp);            
  405.         return false;
  406.     }//if
  407.     return true;
  408. }//febeBoxLogout()
  409.  
  410. function febeResetBN(){
  411.     febeClearLogin()
  412.     document.getElementById("bn_username").disabled = false;
  413.     document.getElementById("bn_username").value = "";
  414.     document.getElementById("bn_password").disabled = false;
  415.     document.getElementById("bn_password").value = "";
  416.     
  417.     document.getElementById("bn_test").disabled = true;
  418.     document.getElementById("bn_upload").disabled = true;
  419.     document.getElementById("bn_view").disabled = true;
  420.     document.getElementById("bn_save").disabled = false;
  421.     document.getElementById("box_net_autoupload").disabled = true;
  422.     document.getElementById("box_net_autoupload").checked = false;
  423.     febePrefs.setBoolPref("extensions.febe.boxnet.autoupload",false);
  424.     
  425.     febePrefs.setCharPref("extensions.febe.boxnet.username","");
  426.     febePrefs.setCharPref("extensions.febe.boxnet.password","");
  427.     
  428.     document.getElementById("bn_username").focus();
  429. }//febeResetBN()
  430.  
  431. function febeSaveBN(){
  432.     var uName = document.getElementById("bn_username").value;
  433.     var password = document.getElementById("bn_password").value;
  434.     if(password == "" || uName == ""){
  435.         febeAlert(febeMsg[187]);
  436.         if(uName == "") document.getElementById("bn_username").focus();
  437.         if(password == "") document.getElementById("bn_password").focus();
  438.         if(password == "" || uName == "") document.getElementById("bn_username").focus();
  439.         return false;
  440.     }//if
  441.     var loginInfo = new febeLoginMgrObj(uName, password);
  442.     Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager).addLogin(loginInfo);
  443.     
  444.     febePrefs.setCharPref("extensions.febe.boxnet.username",uName);
  445.     febePrefs.setCharPref("extensions.febe.boxnet.password","<encrypted>");
  446.     document.getElementById("bn_test").disabled = false;
  447.     document.getElementById("bn_upload").disabled = false;
  448.     document.getElementById("bn_view").disabled = false;
  449.     document.getElementById("bn_save").disabled = true;
  450.     document.getElementById("box_net_autoupload").disabled = false;
  451.     return true;
  452. }//febeSaveBN()
  453.  
  454. function febeClearLogin(){
  455.   // Remove all FEBE box.net logins
  456.     var hostname = 'chrome://FEBE {Box.net login}';
  457.     var formSubmitURL = 'Box.net login';
  458.     var httprealm = null;
  459.     var passwordManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager);
  460.     var logins = passwordManager.findLogins({}, hostname, formSubmitURL, httprealm);
  461.       
  462.     for (var i = 0; i < logins.length; i++) {
  463.         if (logins[i].hostname == hostname) {
  464.             passwordManager.removeLogin(logins[i]);
  465.             break;
  466.         }//if
  467.     }//for
  468.     return true;
  469. }//febeClearLogin()
  470.  
  471. function febeViewBoxFolder(){
  472.     febeBoxLogin();
  473.     febeOpenLink('http://www.box.net/files');
  474.     return true;
  475. }//febeViewBoxFolder()
  476.  
  477. function febeBoxLogin(){
  478.     var prefName = "extensions.febe.boxnet.username";
  479.     var bnUsername = febePrefs.getCharPref(prefName); 
  480.     var bnPassword = febeGetBNpassword(bnUsername);
  481.     var method = "";
  482.     var k = "&api_key=" + febeBoxKey;
  483.     var l = "&login=" + encodeURIComponent(bnUsername);
  484.     var p = "&password=" + encodeURIComponent(bnPassword); 
  485.     var m = "&method=";
  486.  
  487.     var req = new XMLHttpRequest(); 
  488.     req.open('GET', 'http://www.box.net/api/1.0/rest?action=authorization' + k + l + p + m, false); 
  489.     req.onreadystatechange = function (aEvt) {
  490.         var r = req.readyState;
  491.         var s = req.status;
  492.         if (req.readyState != 4 || req.status != 200) {
  493.             var tmp="<style>color: red; font-weight: bold; font-size: 15px;</style>"+febeMsg[188]+"\n"+ febeMsg[189]+" "+resStatus;
  494.             febeAlert(tmp);            
  495.             return false;
  496.         }//if
  497.     };
  498.     req.send(null);
  499.     var results = req.responseText;
  500.     febeAuthToken = febeParseResp(results,"auth_token");
  501.     return results;
  502. }//febeBoxLogin()